package FIT::TypeAdapter::Num;

use FIT::Class;

extends 'FIT::TypeAdapter::Base';

my $Compare_Prefix = '^(?:==)|(?:>=)|(?:<=)|(?:\!=)|(?:(?:>)|(?:<)|(?:=))';

my %Compare_Map = (
    '==' => 'eq',
    '='  => 'eq',
    '!=' => 'ne',
    '<=' => 'le',
    '>=' => 'ge',
    '<'  => 'lt',
    '>'  => 'gt',
);

sub parse_from_string {
    my $num = shift;
    for ($num) {
        s/^\s+//;
        s/\s+$//;
        s/$Compare_Prefix//;
        s/^\s+//;
    }
    return $num;
}

sub compare {
    my ($actual, $expected, $cell) = @_;
    return $self->SUPER::compare($actual, $expected)
        if !defined($actual) || !defined($expected);
    my $method;
    unless (defined $cell) {
        $method = 'eq';
    } else {
        $cell->fit_text =~ /($Compare_Prefix)/;
        $method = $Compare_Map{ $1 || '==' };
    }
    return $self->$method($actual, $expected);
}

sub eq { $_[0] == $_[1] }
sub ne { $_[0] != $_[1] }
sub lt { $_[0] <  $_[1] }
sub gt { $_[0] >  $_[1] }
sub le { $_[0] <= $_[1] }
sub ge { $_[0] >= $_[1] }

